package zhouyang;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 阻塞：在多线程领域，某些情况下会挂起线程(即阻塞)，一旦条件满足，被挂起的
 * 线程又会自动被唤起
 * 必须要阻塞/不得不阻塞
 * 当队列是空的，从队列中获取元素的操作将会被阻塞
 * 当队列是满的，从队列中添加元素的操作将会被阻塞
 */
public class BlockingQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String>blockingQueue=new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add("a"));;
        System.out.println(blockingQueue.add("b")); ;
        System.out.println(blockingQueue.add("c"));  ;
//        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.add("a"));
//        System.out.println(blockingQueue.add("b"));
//        System.out.println(blockingQueue.element());//查看队列第一个元素
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("x"));//false，容量3个，加4个



    }  
}
